<form action="index.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
<script type="text/javascript">
jQuery(document).ready(
  function(){
    jQuery("#adminForm").validate({
      rules:  {
        firstname: "required",
        lastname: "required",
        date_of_birth: {
          required: true,
          date: true
        },
        height: "required",
        weight: "required",
        jersey_number: {
          required: true,
          digits: true
        },
        date_start:{
          required: true,
          date: true
        },
        date_end:{
          required: true,
          date: true
        },
        pic_url: "required",
      }
    });
  }
);
</script>
  <fieldset class="adminform">
    <legend>Details</legend>
      <? 
          $persons        = null;
        if(isset($this->persons))
		  $persons    = $this->persons;
		
        $person_id    = null;
        if(isset($this->person))
          $person_id    = $this->person->id;
        
        $id        = null;
        $team_id     = null;
        $jersey_number   = null;
        $position_id   = null;
        $date_start   = null;
        $date_end     = null;
        $published    = true;
        
        $divisions    = null;
        if(isset($this->divisions))
          $divisions  = $this->divisions;
          
        $positions    = null;
        if(isset($this->positions))
          $positions  = $this->positions;
        
        if(isset($this->player)){
          $id        = $this->player->id;
          $team_id     = $this->player->team_id;
          $jersey_number   = $this->player->jersey_number;
          $position_id   = $this->player->position_id;
          $date_start   = strftime('%Y-%m-%d', strtotime($this->player->date_start));
          $date_end     = strftime('%Y-%m-%d', strtotime($this->player->date_end));
          $published     = $this->player->published;
        }
        
        $pic_id    = null;
        $flickr_id = null;
        $pic_url   = null;
        if(isset($this->pic)){
          $pic_id  = $this->pic->id;
          $flickr_id = $this->pic->flickr_id;
          $pic_url = PBNYHelper::getFlickrImageUrl($flickr_id);
        }
        
        $feature_flickr_id = null;
        $feature_pic_id    = null;
        $feature_pic_url   = null;
        if(isset($this->feature_pic)){
          $feature_pic_id    = $this->feature_pic->id;
          $feature_flickr_id = $this->feature_pic->flickr_id;
          $feature_pic_url   = PBNYHelper::getFlickrImageUrl($feature_flickr_id);
        }
        
      ?>
            <table class="admintable">
        <tr>
          <td class="key">Person</td>
          <td>
            <?
              $options = array();
              foreach($persons as $p)
                array_push($options, JHTML::_('select.option', $p->id, strtoupper("$p->lastname, $p->firstname")));
            ?>
            <?= JHTML::_('select.genericlist',
                $options, 'person_id', null, 'value', 'text', $person_id); ?>
          </td>
        </tr>
        <tr>
          <td></td>
          <td><div id="preview"><img src="<?= $pic_url ?>" alt="<?= $pic_url ?>"></div></td>
          <td></td>
          <td><div id="preview"><img src="<?= $feature_pic_url ?>" alt="<?= $feature_pic_url ?>"></div></td>
        </tr>
        <tr>
          <td class="key">Profile Pic</td>
          <td>
		    <input type="hidden" name="old_pic_url" id="old_pic_url" value="<?= $pic_url ?>"/>
            <input type="hidden" name="pic_flickr_id" id="pic_flickr_id" value="<?= $flickr_id ?>"/>
            <textarea id="pic_url" name="pic_url" rows="2" cols="40"><?= $pic_url ?></textarea>
          </td>
          <td class="key">Feature Pic</td>
          <td>
		    <input type="hidden" name="old_feature_pic_url" id="old_feature_pic_url" value="<?= $feature_pic_url ?>"/>
            <input type="hidden" name="feature_pic_flickr_id" id="feature_pic_flickr_id" value="<?= $feature_flickr_id ?>"/>
            <textarea id="feature_pic_url" name="feature_pic_url" rows="2" cols="40"><?= $feature_pic_url ?></textarea>
          </td>
        </tr>
        <tr valign="top">
          <td class="key">Divisions</td>
          <td>
            <? $options = array();
               foreach($divisions as $d)
                 array_push($options, JHTML::_('select.option', $d->id, strtoupper($d->name)));
               $event = "onchange=\"ajx_request('".JRequest::getVar('c', 'player')."', 'get_team_instances',  'division_id='+this.value, 'teams_container')\" ";
               $attr  = $event." size=".count($options);
            ?>
            
            <?= JHTML::_('select.genericlist', $options, 'division_id', $attr, 'value', 'text');?>
          </td>
          <td class="key">Teams</td>
          <td>
            <div id="teams_container">
                <? if(!isset($this->teams)): ?>
                <select id='team_id' name='team_id'></select>
              <? else: ?>
                <?= JHTML::_('select.genericlist', $this->teams, 'team_id', $event." size=".count($options), 'id', 'name', $team_id);?>
              <? endif; ?>
            </div>
          </td>
        </tr>
        <tr>
                    <td class="key">Jersey Number</td>
          <td>
            <input type="text" name="jersey_number" id="jersey_number" value="<?= $jersey_number; ?>" />
          </td>
        </tr>
        <tr>
                    <td class="key">Position</td>
          <td>
            <?
              $options  = array();
              for($i=0; $i< count($positions);$i++){
                $position = $positions[$i];
                array_push($options, JHTML::_('select.option', $position->id, strtoupper($position->position)));
              }
            ?>
            <?= JHTML::_('select.genericlist', $options, 'position_id', null, 'value', 'text', $position_id);?>
          </td>
        </tr>
        <tr>
          <td class="key">Date Start</td>
          <td>
            <?= JHTML::calendar($date_start, "date_start", "date_start") ?>
          </td>
        </tr>
        <tr>
          <td class="key">Date End</td>
          <td>
            <?= JHTML::calendar($date_end, "date_end", "date_end") ?>
          </td>
        </tr>
        <tr>
          <td class="key">Published</td>
          <td>
            <?= JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $published); ?>
          </td>
        </tr>
             </table>
       </fieldset>
       <input type="hidden" name="option" value="<?= JRequest::getVar( 'option' );?>"/>
       <input type="hidden" name="c" value="<?= JRequest::getVar('c', 'season'); ?>" />
       <input type="hidden" name="id" value="<?= $id; ?>"/>
       <input type="hidden" name="task" value=""/>
     
       <input type="hidden" name="pic_id" value="<?= $pic_id ?>"/>
       <input type="hidden" name="feature_pic_id" value="<?= $feature_pic_id ?>"/>
</form>